package com.neoCalf.neoUser.controller;

import com.neoCalf.neoCommon.entity.SysSetting;
import com.neoCalf.neoCommon.entity.UserInfo;
import com.neoCalf.neoCommon.enums.ResultCode;
import com.neoCalf.neoCommon.model.R;
import com.neoCalf.neoCommon.model.dto.TokenUserInfoDto;
import com.neoCalf.neoCommon.redis.RedisComponent;
import com.neoCalf.neoCommon.redis.RedisTool;
import com.neoCalf.neoCommon.utils.JwtUtil;
import com.neoCalf.neoUser.service.LoginService;
import com.neoCalf.neoUser.webSocket.tool.ChannelContextUtils;
import io.jsonwebtoken.Claims;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;


/**
 * @author 小牛
 * @version 1.0
 * @description login
 * @date 2025/4/8 3:17
 */
@RestController
@RequestMapping("/public")
@Validated
public class LoginController {
    @Resource
    private LoginService loginService;
    @Resource
    private RedisTool<String> redisTool;
    @Resource
    private ChannelContextUtils channelContextUtils;
    @Resource
    private JwtUtil<Long> jwtUtil;

    @PostMapping("/login")
    public R<TokenUserInfoDto> login(@RequestBody @NotNull UserInfo userInfo) {
        TokenUserInfoDto tokenUserInfoDto = loginService.login(userInfo.getEmail(), userInfo.getPassword());
        return R.ok(tokenUserInfoDto);
    }

    @PostMapping("/register")
    public R<String> register(@RequestBody @NotNull UserInfo userInfo,
                              @RequestParam @NotEmpty String checkCode) {
        loginService.register(userInfo, checkCode);
        return R.ok();
    }

    @GetMapping(value = "forgetPass")
    public R<String> forget(@NotEmpty @Email String email,
                            @NotEmpty String password,
                            @NotEmpty String checkCode) {
        loginService.forget(email, password, checkCode);
        return R.ok();
    }

    @PostMapping("/phoneLogin")
    public R<TokenUserInfoDto> phoneLogin(@RequestParam("phone") @NotEmpty String phone, @RequestParam("checkCode") @NotEmpty String checkCode) {
        TokenUserInfoDto tokenUserInfoDto = loginService.phoneLogin(phone, checkCode);
        return R.ok(tokenUserInfoDto);
    }

    @GetMapping("/sysSetting")
    public R<SysSetting> getSysSetting() {
        SysSetting sysSetting = loginService.getSysSetting();
        return R.ok(sysSetting);
    }

    @GetMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        try {
            String token = request.getHeader("token");
            Claims claims = jwtUtil.parseJwtToken(token);
            if (claims == null) {
                return R.fail(ResultCode.CODE_10017);
            }
            String userId = (String) claims.get("userId");
            channelContextUtils.logout(userId);
            redisTool.delete(token);
        } catch (Exception ignore) {
            R.fail(ResultCode.CODE_10017);
        }
        return R.ok();
    }
}
